﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FileDFS.Utils.PathConfig;
using System.IO;
using FileDFS.Model;
using FileDFS.Model.Enum;
/*
 * Coder:Tieria
 * WriteTime:2015.5.15
 * FileName:FolderSizeHandler.cs
 * ReMark:获取文件夹里面存放的文件大小的详情
 */
namespace FileDFS.Bll
{
    public class FolderSizeHandler
    {
        public static FolderSizeHandler GetInstance()
        {
            return new FolderSizeHandler();
        }
        #region 获取文件集合的总大小
        public double GetListFilesSize(List<string> virtualPathList)
        {
            double sizeCount = 0.00;
            foreach (var item in virtualPathList)
            {
                sizeCount += GetFileSize(item);
            }
            return sizeCount;
        } 
        #endregion

        #region 获取某个文件的大小
        public double GetFileSize(string virtualFullPath)
        {
            PathDetail pathDetail = CatalogHandler.GetInstance().GetDetailByVirtual(virtualFullPath);
            FileInfo fileInfo = new FileInfo(pathDetail.Absolute_FullPath);
            return fileInfo.Length / 1024;

        } 
        #endregion

        #region 获取当前服务器节点全部的配置目录
        public List<FolderInfoModel> GetAllFolders()
        {
            List<FolderInfoModel> folderList = new List<FolderInfoModel>();
            AllPathConfig config = CatalogHandler.GetInstance().GetAllFilePath();
            var filePrivate = GetFolderList(config.filePathConfig.privatePath.optionsCollection.Cast<PathOption>(), FolderTypeEnum.FileFolder, false);
            var filePublic = GetFolderList(config.filePathConfig.publicPath.optionsCollection.Cast<PathOption>(), FolderTypeEnum.FileFolder, true);
            var imgPrivate = GetFolderList(config.imagePathConfig.privatePath.optionsCollection.Cast<PathOption>(), FolderTypeEnum.ImageFolder, false);
            var imgPublic = GetFolderList(config.imagePathConfig.publicPath.optionsCollection.Cast<PathOption>(), FolderTypeEnum.ImageFolder, true);
            folderList = filePrivate.Union(filePublic).Union(imgPublic).Union(imgPrivate).ToList();
            return folderList;
        }
        /// <summary>
        /// 转换
        /// </summary>
        /// <param name="optionList"></param>
        /// <param name="floderType"></param>
        /// <param name="isPublic"></param>
        /// <returns></returns>
        private List<FolderInfoModel> GetFolderList(IEnumerable<PathOption> optionList, FolderTypeEnum floderType, bool isPublic)
        {
            List<FolderInfoModel> folderList = new List<FolderInfoModel>();
            foreach (var item in optionList)
            {
                FolderInfoModel folder = new FolderInfoModel()
                {
                    Driver = item.AbsoluteDriver,
                    AbsolutePath = item.AbsolutePath,
                    VirtualFullPath = item.VirtualPath,
                    FolderType = FolderTypeEnum.FileFolder,
                    IsPublic=isPublic
                };
                folderList.Add(folder);
            }
            return folderList;
        }

        #endregion
    
    }
}
